function [ is_constant_speed,states ] = ConstantSpeedClassifier( speed,accl,brake,is_constant_speed,states)
% Detact constant speed

%-------------------------TUNNING FACTORS----------------------------------
 Accel_Max = 0.08;
 Accel_Min = -0.08; % acceleration upper & lower bound 
 Speed_Min = 20; % minimal speed
 Buffer = 10;    %To prevent frequent changing of states 
%--------------------------------------------------------------------------    
    
counter = states;
    
%-------------Finite State Machine ----------------------------------------
    switch is_constant_speed
        case false 
         % Not constant speed state
             if (accl<Accel_Max) && (accl>Accel_Min) && (brake==0) && (speed>Speed_Min) 
                   counter=counter+1;
                   if counter> Buffer            
                        is_constant_speed= true;  % swtich to constant speed state
                        counter=0;   
                   end
             end
       
         case true  
         % Constant Speed state
               if (accl<Accel_Min) ||(accl>Accel_Max) || (brake==1)
                   is_constant_speed= false; % swtich to neutral state
               end
    end   
states =  counter ;

end

